<template>
  <div class="player-profile">
    <div class="profile-header">
      <div class="avatar-section">
        <img 
          :src="player.avatar || '/default-avatar.png'" 
          :alt="player.nickname"
          class="avatar"
        />
        <div class="level-badge">
          <span class="level">{{ getLevelName(player.level) }}</span>
          <div class="level-progress">
            <div class="progress-bar" :style="{ width: player.levelProgress + '%' }"></div>
          </div>
        </div>
      </div>
      
      <div class="info-section">
        <h2 class="player-name">{{ player.nickname }}</h2>
        <p class="player-username">@{{ player.username }}</p>
        <div class="player-status">
          <span :class="['status-indicator', player.status]"></span>
          <span>{{ getStatusName(player.status) }}</span>
        </div>
        
        <div class="player-stats-overview">
          <div class="stat-item">
            <span class="stat-value">{{ player.stats.totalScore }}</span>
            <span class="stat-label">总分</span>
          </div>
          <div class="stat-item">
            <span class="stat-value">{{ player.stats.solvedCases }}</span>
            <span class="stat-label">破案数</span>
          </div>
          <div class="stat-item">
            <span class="stat-value">{{ player.stats.accuracy }}%</span>
            <span class="stat-label">准确率</span>
          </div>
        </div>
      </div>
      
      <div class="currency-section">
        <div class="currency-item">
          <span class="currency-icon">💰</span>
          <span class="currency-value">{{ player.coins }}</span>
        </div>
        <div class="currency-item">
          <span class="currency-icon">💎</span>
          <span class="currency-value">{{ player.gems }}</span>
        </div>
      </div>
    </div>
    
    <div class="profile-content">
      <div class="stats-section">
        <h3>详细统计</h3>
        <div class="stats-grid">
          <div class="stat-card">
            <div class="stat-title">总案件数</div>
            <div class="stat-number">{{ player.stats.totalCases }}</div>
          </div>
          <div class="stat-card">
            <div class="stat-title">破案率</div>
            <div class="stat-number">{{ getSolveRate }}%</div>
          </div>
          <div class="stat-card">
            <div class="stat-title">当前连胜</div>
            <div class="stat-number">{{ player.stats.currentStreak }}</div>
          </div>
          <div class="stat-card">
            <div class="stat-title">最高连胜</div>
            <div class="stat-number">{{ player.stats.maxStreak }}</div>
          </div>
          <div class="stat-card">
            <div class="stat-title">平均分</div>
            <div class="stat-number">{{ player.stats.averageScore }}</div>
          </div>
          <div class="stat-card">
            <div class="stat-title">平均用时</div>
            <div class="stat-number">{{ formatTime(player.stats.averageTimePerCase) }}</div>
          </div>
        </div>
      </div>
      
      <div class="achievements-section">
        <h3>成就</h3>
        <div class="achievements-list">
          <div 
            v-for="achievement in player.achievementList" 
            :key="achievement"
            class="achievement-item"
          >
            🏆 {{ achievement }}
          </div>
          <div v-if="player.achievementList.length === 0" class="no-achievements">
            暂无成就
          </div>
        </div>
      </div>
      
      <div class="preferences-section">
        <h3>偏好设置</h3>
        <div class="preferences-grid">
          <div class="preference-item">
            <span class="preference-label">主题</span>
            <span class="preference-value">{{ player.preferences.theme }}</span>
          </div>
          <div class="preference-item">
            <span class="preference-label">语言</span>
            <span class="preference-value">{{ player.preferences.language }}</span>
          </div>
          <div class="preference-item">
            <span class="preference-label">通知</span>
            <span class="preference-value">{{ player.preferences.notifications ? '开启' : '关闭' }}</span>
          </div>
          <div class="preference-item">
            <span class="preference-label">音效</span>
            <span class="preference-value">{{ player.preferences.soundEffects ? '开启' : '关闭' }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import type { Player } from '@/entities/Player';
import { 
  getPlayerLevelName, 
  getPlayerStatusName,
  PlayerLevel,
  PlayerStatus 
} from '@/entities/Player';

interface Props {
  player: Player;
}

const props = defineProps<Props>();

// 计算属性
const getSolveRate = computed(() => {
  const stats = props.player.stats;
  const total = stats.solvedCases + stats.failedCases;
  return total > 0 ? Math.round((stats.solvedCases / total) * 100) : 0;
});

// 方法
const getLevelName = (level: PlayerLevel) => {
  return getPlayerLevelName(level);
};

const getStatusName = (status: PlayerStatus) => {
  return getPlayerStatusName(status);
};

const formatTime = (seconds: number) => {
  if (seconds < 60) return `${seconds}秒`;
  if (seconds < 3600) return `${Math.floor(seconds / 60)}分钟`;
  return `${Math.floor(seconds / 3600)}小时${Math.floor((seconds % 3600) / 60)}分钟`;
};
</script>

<style scoped>
.player-profile {
  background: var(--color-background);
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.profile-header {
  display: flex;
  align-items: center;
  gap: 24px;
  padding-bottom: 24px;
  border-bottom: 1px solid var(--color-border);
  margin-bottom: 24px;
}

.avatar-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  object-fit: cover;
  border: 3px solid var(--color-primary);
}

.level-badge {
  text-align: center;
}

.level {
  font-size: 14px;
  font-weight: 600;
  color: var(--color-primary);
}

.level-progress {
  width: 60px;
  height: 4px;
  background: var(--color-border);
  border-radius: 2px;
  margin-top: 4px;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  background: var(--color-primary);
  border-radius: 2px;
  transition: width 0.3s ease;
}

.info-section {
  flex: 1;
}

.player-name {
  font-size: 24px;
  font-weight: 700;
  margin: 0 0 4px 0;
  color: var(--color-text);
}

.player-username {
  font-size: 14px;
  color: var(--color-text-secondary);
  margin: 0 0 8px 0;
}

.player-status {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 16px;
}

.status-indicator {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #4caf50;
}

.status-indicator.inactive {
  background: #ff9800;
}

.status-indicator.banned {
  background: #f44336;
}

.player-stats-overview {
  display: flex;
  gap: 24px;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.stat-value {
  font-size: 18px;
  font-weight: 600;
  color: var(--color-primary);
}

.stat-label {
  font-size: 12px;
  color: var(--color-text-secondary);
}

.currency-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.currency-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  background: var(--color-background-soft);
  border-radius: 6px;
}

.currency-icon {
  font-size: 16px;
}

.currency-value {
  font-weight: 600;
  color: var(--color-text);
}

.profile-content {
  display: grid;
  gap: 24px;
}

.stats-section h3,
.achievements-section h3,
.preferences-section h3 {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 16px 0;
  color: var(--color-text);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 16px;
}

.stat-card {
  background: var(--color-background-soft);
  padding: 16px;
  border-radius: 8px;
  text-align: center;
}

.stat-title {
  font-size: 14px;
  color: var(--color-text-secondary);
  margin-bottom: 8px;
}

.stat-number {
  font-size: 24px;
  font-weight: 700;
  color: var(--color-primary);
}

.achievements-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.achievement-item {
  background: var(--color-background-soft);
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 14px;
  color: var(--color-text);
}

.no-achievements {
  color: var(--color-text-secondary);
  font-style: italic;
}

.preferences-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
}

.preference-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px;
  background: var(--color-background-soft);
  border-radius: 6px;
}

.preference-label {
  font-weight: 500;
  color: var(--color-text);
}

.preference-value {
  color: var(--color-text-secondary);
}

@media (max-width: 768px) {
  .profile-header {
    flex-direction: column;
    text-align: center;
  }
  
  .player-stats-overview {
    justify-content: center;
  }
  
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}
</style>